#include "FiveWin.ch"

static nTotal      // Total number of items first time read
static cIniFile
static oMenu

//----------------------------------------------------------------------------//

function ToolsSetup()

   local oIni, oDlg, oLbx, oName, oParams, oPrompt
   local aTools := {}, cNewTool
   local cTool := "Not defined", cItem, cName, cParams, cPrompt := ""
   local aNames := { nil }
   local lEnd   := .f.
   local n      := 1
   local cTemp

   INI oIni FILENAME cIniFile
      while ! Empty( cTool )
         if n == 1
            GET cTool SECTION "Tools" ENTRY "1" ;
               DEFAULT "Not defined,,,," OF oIni
         else
            GET cTool SECTION "Tools" ;
               ENTRY Str( n, If( n < 10, 1, 2 ) ) OF oIni
         endif
         if ! Empty( cTool )
            AAdd( aNames, cTool )
            AAdd( aTools, StrToken( cTool, 1, "," ) )
         endif
         n++
      end
   ENDINI

   nTotal = n - 1

   DEFINE DIALOG oDlg RESOURCE "Tools"

   REDEFINE LISTBOX oLbx VAR cTool ITEMS aTools ;
      ID 110 OF oDlg ;
      ON CHANGE cItem := aNames[ oLbx:GetPos() ],;
                cName := PadR( StrToken( cItem, 2, "," ), 50 ),;
                oName:Refresh(),;
                cParams := PadR( StrToken( cItem, 3, "," ), 20 ),;
                oParams:Refresh(),;
                cPrompt := PadR( StrToken( cItem, 1, "," ), 20 ),;
                oPrompt:Refresh()

   REDEFINE BUTTON ID 120 OF oDlg ;
      ACTION oLbx:SwapUp()

   REDEFINE BUTTON ID 130 OF oDlg ;
      ACTION oLbx:SwapDown()

   REDEFINE BUTTON ID 140 OF oDlg ;
      ACTION AddTool( oLbx, aNames )

   REDEFINE BUTTON ID 150 OF oDlg ;
      ACTION ADel( aNames, oLbx:GetPos() ),;
             ASize( aNames, Len( aNames ) - 1 ),;
             oLbx:Del( oLbx:GetPos() )

   REDEFINE GET oName   VAR cName   ID 160 OF oDlg
   REDEFINE GET oParams VAR cParams ID 170 OF oDlg

   REDEFINE GET oPrompt VAR cPrompt ID 180 OF oDlg ;
      VALID ( cPrompt := AllTrim( cPrompt ), n := oLbx:GetPos(),;
              If( n != 0, ( oLbx:Modify( cPrompt, n ),;
              aNames[ n ] := cPrompt + ;
              SubStr( aNames[ n ], At( ",", aNames[ n ] ) ) ),),;
              .t. )

   REDEFINE BUTTON ID ID_OK OF oDlg ;
      ACTION SaveTools( aNames ), MenuSetTools(), oDlg:End()

   ACTIVATE DIALOG oDlg CENTER

return nil

//----------------------------------------------------------------------------//

static function AddTool( oLbx, aNames )

   local cApp := Lower( cGetFile( "*.exe", "Select a tool" ) )
   local cPrompt

   if ! Empty( cApp ) .and. File( cApp )
      cPrompt = StrCapFirst( cFileNoPath( cFileNoExt( cApp ) ) )
      oLbx:Add( cPrompt )
      AAdd( aNames, cPrompt + "," + cApp )
      oLbx:GoBottom()
   endif

return nil

//----------------------------------------------------------------------------//

static function SaveTools( aNames )

   local oIni, n

   INI oIni FILENAME cIniFile
      for n = 1 to Len( aNames )
         SET SECTION "Tools" ENTRY Str( n, If( n < 10, 1, 2 ) ) ;
            TO aNames[ n ] OF oIni
      next
      for n = Len( aNames ) + 1 to nTotal
         SET SECTION "Tools" ENTRY Str( n, If( n < 10, 1, 2 ) ) ;
            TO "" OF oIni
      next
   ENDINI

return nil

//----------------------------------------------------------------------------//

function MenuAddTools( cIni )

   local oItem, cTool, n

   cIniFile = cIni

   MENUITEM "&Tools"
   MENU oMenu
      n = 1
      while ! Empty( cTool := GetPvProfString( "Tools",;
              Str( n, If( n < 10, 1, 2 ) ),, cIniFile ) )
         MENUITEM oItem PROMPT StrToken( cTool, 1, "," ) ;
            MESSAGE StrToken( cTool, 2, "," )
         oItem:bAction = &( '{|| WinExec( "' + ;
            StrToken( cTool, 2, "," ) + '" )}' )
         n++
      end

      SEPARATOR

      MENUITEM "&Settings..." MESSAGE "User defined tools" ;
         ACTION ToolsSetup() RESOURCE "Tools"
   ENDMENU

return oMenu

//----------------------------------------------------------------------------//

function MenuSetTools()

   local oItem, cTool, n

   oMenu:DelItems()
   SysRefresh()

   n = 1
   while ! Empty( cTool := GetPvProfString( "Tools",;
           Str( n, If( n < 10, 1, 2 ) ),, cIniFile ) )
      oItem = TMenuItem():New( StrToken( cTool, 1, "," ),;
                               StrToken( cTool, 2, "," ) )
      oItem:bAction = &( '{|| WinExec( "' + ;
         StrToken( cTool, 2, "," ) + '" )}' )
      oMenu:Add( oItem )
      n++
   end

   oMenu:Add( TMenuItem():New() )
   oMenu:Add( oItem := TMenuItem():New( "&Settings...",;
       "User defined tools" ) )
   oItem:bAction = { || ToolsSetup() }

return nil

//----------------------------------------------------------------------------//